package sddz.foodmanage.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.app.Fragment;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.BitmapImageViewTarget;

import java.io.File;

import sddz.foodmanage.R;

/**
 * Created by zhanglei on 2017/4/14.
 * 图片加载类
 */

public class ImageLoader {

    public static void loadImageUrl1(Activity activity, String url, ImageView imageView) {
        Glide.with(activity).load(url).into(imageView);
    }
    public static void loadImageUrl1(Context activity, String url, ImageView imageView) {
        Glide.with(activity).load(url).into(imageView);
    }

//    public static void loadImageUrl1(Fragment activity, String url, ImageView imageView) {
//        Glide.with(activity).load(url).into(imageView);
//    }
//
//    public static void loadImageUrl(Context activity, String url, ImageView imageView) {
//        Glide.with(activity).load(url).into(imageView);
//    }
//
//    public static void loadImageFile(Context activity, String filePath, ImageView imageView) {
//        File file = new File(filePath);
//        if (file.exists()){
//            Glide.with(activity).load(file).into(imageView);
//        }
//    }
    public static void loadImageUrl(Activity activity, String url, ImageView imageView) {
        Glide.with(activity).load(API.BASEURLIMAGE + url)
                .placeholder(R.mipmap.icon_load)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }

    public static void loadImageUrl(Fragment activity, String url, ImageView imageView) {
        Glide.with(activity).load(API.BASEURLIMAGE + url)
                .placeholder(R.mipmap.icon_load)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }

    public static void loadImageUrl(Context activity, String url, ImageView imageView) {
        Glide.with(activity).load(API.BASEURLIMAGE + url)
                .placeholder(R.mipmap.icon_load)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }

    public static void loadImageFile(Activity activity, String filePath, ImageView imageView) {
        Glide.with(activity).load(new File(filePath))
                .placeholder(R.mipmap.icon_load)
                .into(imageView);
    }


    /**
     * 加载圆形网络图片
     *
     * @param activity
     * @param url
     * @param imageView
     */
    public static void loadRoundImageUrl(final Activity activity, String url, final ImageView imageView) {
        String imageUrl = API.BASEURLIMAGE + url;
        Glide.with(activity).load(imageUrl)
                .asBitmap()
                .placeholder(R.mipmap.icon_load)
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(new BitmapImageViewTarget(imageView) {
                    @Override
                    protected void setResource(Bitmap resource) {
                        RoundedBitmapDrawable circularBitmapDrawable =
                                RoundedBitmapDrawableFactory.create(activity.getResources(), resource);
                        circularBitmapDrawable.setCircular(true);
                        imageView.setImageDrawable(circularBitmapDrawable);
                    }
                });
    }

    /**
     * 加载圆形本地图片
     *
     * @param activity
     * @param filePath
     * @param imageView
     */
    public static void loadRoundImageFile(final Activity activity, String filePath, final ImageView imageView) {
        Glide.with(activity).load(new File(filePath))
                .asBitmap().centerCrop().into(new BitmapImageViewTarget(imageView) {
            @Override
            protected void setResource(Bitmap resource) {
                RoundedBitmapDrawable circularBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(activity.getResources(), resource);
                circularBitmapDrawable.setCircular(true);
                imageView.setImageDrawable(circularBitmapDrawable);
            }
        });
    }

    public static void loadImageURl(Activity activity, Bitmap bitmap, ImageView view) {
    }
}
